package han.cirno.corrupt.activity

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import han.cirno.corrupt.Application
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject

@HiltViewModel
class GlobalViewModel @Inject constructor() :ViewModel() {
    val broadcastCheckState=MutableStateFlow(-1)

    val broadcastCheck: MutableLiveData<Int> = MutableLiveData(-1)
    val configLatitude:MutableLiveData<String> =MutableLiveData("-")
    val configLongitude:MutableLiveData<String> = MutableLiveData("-")
    val configMode:MutableLiveData<String> =MutableLiveData("-")

    companion object {
        private fun clearAll() {
            val globalViewModel = Application.application.globalViewModel
            globalViewModel.configMode.postValue("-")
            globalViewModel.configLatitude.postValue("-")
            globalViewModel.configLongitude.postValue("-")
            globalViewModel.broadcastCheckState.value=-1
            globalViewModel.broadcastCheck.postValue(-1)
        }

        private fun getBaseIntent():Intent{
            val intent= Intent("han.cirno.nofy.broadcast.BROADCAST")
            intent.setPackage("han.cirno.nofy")
            intent.setComponent(ComponentName("han.cirno.nofy","han.cirno.nofy.NofyReceiver"))
            return intent
        }

        fun sendCheckBroadcast(context: Context) {
            clearAll()
            val intent = getBaseIntent()
            val bundle = Bundle()
            bundle.putString("type","type_status")
            bundle.putString("package",context.packageName)
            intent.putExtras(bundle)
            context.sendBroadcast(intent)
        }

        fun sendEditBroadcast(context: Context,longitude:Double,latitude:Double){
            val intent = getBaseIntent()
            val bundle = Bundle()
            bundle.putString("type","type_set")
            bundle.putString("latitude",latitude.toString())
            bundle.putString("longitude",longitude.toString())
            bundle.putString("package",context.packageName)
            intent.putExtras(bundle)
            context.sendBroadcast(intent)
        }
    }
}
